package org.aceor.mddal.common.sqlparser.result.object;

import com.google.common.base.Objects;
import com.google.common.base.Strings;

import java.util.List;

/**
 * Created by lxue on 16/3/23.
 */
public class AggregationColumn {
    private final AggregationType aggregationType;

    private String expression;
    private List<String> arguments;
    private String alias;

    public AggregationColumn(AggregationType aggregationType,List<String> arguments, String expression) {
        this.aggregationType = aggregationType;
        this.arguments = arguments;
        this.expression = expression;
    }

    public AggregationType getAggregationType() {
        return aggregationType;
    }

    public List<String> getArguments() {
        return arguments;
    }

    public String getAlias() {
        return alias;
    }

    public String getExpression() {
        //别名优先
        //TODO: 别名还没能设置
        return Strings.isNullOrEmpty(alias) ? expression : alias;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .add("arguments", arguments)
                .add("expression", expression)
                .add("type", aggregationType)
                .add("alias", alias)
                .toString();
    }

    /**
     * 聚合函数类型.
     *
     * @author gaohongtao
     */
    public enum AggregationType {
        MAX, MIN, SUM, COUNT, AVG;

        public static AggregationType valueof(String name) {
            return valueOf(name.toUpperCase());
        }
    }
}
